/*
 * $Id: SystemInfo.java,v 1.8 2010/10/25 19:49:52 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.pojo.sysInfo;

import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.exceptions.GSCCUtilException;
import com.ge.healthcare.autosc.common.util.GSCCUtil;

/**
 * 
 * @author 212042946
 * 
 */
public class SystemInfo {

	protected Long addressId = null;
	protected String ipAddress = null;
	protected String modality = null;
	protected String modelType = null;
	protected String location = null;
	protected String isoCode = null;
	protected String pole = null;
	protected String usn = null;
	protected String mln = null;
	protected String systemId = null;
	protected String systemType = null;
	public String getSystemType() {
		return systemType;
	}

	public void setSystemType(String systemType) {
		this.systemType = systemType;
	}

	protected String productLine = null;
	protected String connectType = null;
	protected String linkSecret = null;
	protected String modemAreaCityCode = null;
	protected String modemNum = null;
	protected Properties loginInfo = null;
	protected String serviceCenter = null;
	protected String portMaster = null;
	protected boolean systemDetailsExist = false;
	protected boolean connectivityInfoExist = false;
	protected boolean loginInfoExist = false;
	protected boolean allExist = false;
	protected boolean isIP = false;
	protected String insiteEntitlement = null;
	protected String agreementSigned = null;
	protected Date insiteCheckedDate = null;

	protected SystemInfo() {
	}

	public SystemInfo(String systemId) throws GSCCUtilException {
		this.systemId = systemId;
		init(systemId);
		allExist = true;
	}

	public SystemInfo(SystemDetails systemDetail) throws GSCCUtilException {
		systemId = systemDetail.getSystemId();
		usn = systemDetail.getUsn();
		mln = systemDetail.getMln();
		addressId = new Long(systemDetail.getAddressId());
		modality = systemDetail.getModalityCode();
		modelType = systemDetail.getModelTypeCode();
		productLine = systemDetail.getProductLineCode();
		location = systemDetail.getServiceCenterCode();
		pole = systemDetail.getPole();
		isoCode = systemDetail.getIsoCode();
		serviceCenter = systemDetail.getServiceCenterCode();
		systemType = systemDetail.getSystemType();
		insiteEntitlement = systemDetail.getInsiteEntitlement();
		agreementSigned = systemDetail.getAgreementSigned();
		insiteCheckedDate = systemDetail.getInsiteCheckedDate();
		populateConnectivityDetails(systemId);
		populateLoginDetails(systemId);
		allExist = true;
	}


	/**
	 * @return the addressId
	 */
	public Long getAddressId() {
		return addressId;
	}

	/**
	 * @param addressId the addressId to set
	 */
	public void setAddressId(Long addressId) {
		this.addressId = addressId;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getModality() {
		return modality;
	}

	public String getModelType() {
		return modelType;
	}

	public String getLocation() {
		return location;
	}

	public String getPole() {
		return pole;
	}

	public String getIsoCode() {
		return isoCode;
	}

	public String getUsn() {
		return usn;
	}

	public String getMln() {
		return mln;
	}

	public String getSystemId() {
		return systemId;
	}

	public String getProductLine() {
		return productLine;
	}

	public Properties getLoginInfo() {
		return loginInfo;
	}

	public void setLoginInfo(Vector<LoginDetails> systemLogins) {
		String methodName = "setLoginInfo";

		loginInfo = new Properties();
		Iterator<LoginDetails> it = systemLogins.iterator();
		while (it.hasNext()) {
			LoginDetails aLogin = (LoginDetails) it.next();
			String login = aLogin.getLoginId();
			String pass = aLogin.getPassword();
			Boolean modem = aLogin.isModemPassword();
			if (!modem.booleanValue() && (login != null) && (pass != null)) {
				loginInfo.setProperty(login, pass);
			}
		}
		if (loginInfo.size() == 0) {
			ASCLogger.warn(this.getClass(), methodName,
					"No login or password defined for system " + systemId);
			loginInfoExist = false;
		}
	}

	public String getConnectType() {
		return connectType;
	}

	public String getModemAreaCityCode() {
		return modemAreaCityCode;
	}

	public String getModemNum() {
		return modemNum;
	}

	public boolean isSystemInfoExist() {
		return allExist;
	}

	public boolean isSystemDetailsExist() {
		return systemDetailsExist;
	}

	public boolean isConnectivityInfoExist() {
		return connectivityInfoExist;
	}

	public boolean isLoginInfoExist() {
		return loginInfoExist;
	}

	public String toString() {
		StringBuffer msg = new StringBuffer();
		msg.append("{").append("\n");
		msg.append("System Id: ").append(systemId).append("\n");
		msg.append("USN: ").append(usn).append("\n");
		msg.append("MLN: ").append(mln).append("\n");
		msg.append("Modality: ").append(modality).append("\n");
		msg.append("Model type: ").append(modelType).append("\n");
		msg.append("Product line: ").append(productLine).append("\n");
		msg.append("Location: ").append(location).append("\n");
		msg.append("ISO Code: ").append(isoCode).append("\n");
		msg.append("Pole: ").append(pole).append("\n");
		msg.append("Connect Type: ").append(connectType).append("\n");
		msg.append("Modem Area Code: ").append(modemAreaCityCode).append("\n");
		msg.append("Modem No: ").append(modemNum).append("\n");
		msg.append("Service Center Code: ").append(serviceCenter).append("\n");
		msg.append("Port Master: ").append(portMaster).append("\n");
		msg.append("}").append("\n");
		return msg.toString();
	}

	private void init(String systemId) throws GSCCUtilException {
		String methodName = "init";
		try {
			populateSystemDetails(systemId);

			populateConnectivityDetails(systemId);

			populateLoginDetails(systemId);
		} catch (GSCCUtilException gue) {
			// All or none situation
			String msg = "Error building system information for system "
					+ systemId;
			ASCLogger.error(this.getClass(), methodName, msg, gue);
			clear();
			throw gue;
		}
	}

	private void populateSystemDetails(String systemId)
			throws GSCCUtilException {
		String methodName = "populateSystemDetails";
		try {
			SystemDetails systemDetail = GSCCUtil
					.getSystemDetailsForSystemId(systemId);
			if (systemDetail == null) {
				ASCLogger.warn(this.getClass(), methodName,
						"No system found for system " + systemId);
			} else {
				systemId = systemDetail.getSystemId();
				addressId = new Long(systemDetail.getAddressId());
				usn = systemDetail.getUsn();
				mln = systemDetail.getMln();
				modality = systemDetail.getModalityCode();
				modelType = systemDetail.getModelTypeCode();
				productLine = systemDetail.getProductLineCode();
				location = systemDetail.getServiceCenterCode();
				isoCode = systemDetail.getIsoCode();
				pole = systemDetail.getPole();
				serviceCenter = systemDetail.getServiceCenterCode();
				systemDetailsExist = true;
				systemType = systemDetail.getSystemType();
			}
		} catch (GSCCUtilException gue) {
			String msg = "Error retrieving system information for system "
					+ systemId;
			ASCLogger.error(this.getClass(), methodName, msg, gue);
			throw gue;
		}

	}

	private void populateConnectivityDetails(String systemId)
			throws GSCCUtilException {
		String methodName = "populateConnectivityDetails";
		try {
			Vector<ConnectivityDetails> systemConnDetails = GSCCUtil
					.getConnectivityDetailsForSystemId(systemId);
			if (systemConnDetails.size() == 0) {
				ASCLogger.warn(this.getClass(), methodName,
						"No connectivity details found for system " + systemId);
			} else {
				ConnectivityDetails connDetails = (ConnectivityDetails) systemConnDetails
						.firstElement();
				ipAddress = connDetails.getSystemIP();
				connectType = connDetails.getConnType();
				modemNum = connDetails.getModemNo();
				linkSecret = connDetails.getChapSecret();
				modemAreaCityCode = connDetails.getModemAreaCode();
				portMaster = connDetails.getPortMasterName();
				isIP = connDetails.isIP();
				connectivityInfoExist = true;
			}
		} catch (GSCCUtilException gue) {
			String msg = "Error retrieving connectivity information for system "
					+ systemId;
			ASCLogger.error(this.getClass(), methodName, msg, gue);
			throw gue;
		}
	}

	private void populateLoginDetails(String systemId) throws GSCCUtilException {
		String methodName = "populateLoginDetails";
		try {
			Vector<LoginDetails> logins = GSCCUtil
					.getLoginDetailsForSystemId(systemId);
			if (logins.size() == 0) {
				ASCLogger.warn(this.getClass(), methodName,
						"No login details found for system " + systemId);
			} else {
				setLoginInfo(logins);
				loginInfoExist = true;
			}
		} catch (GSCCUtilException gue) {
			String msg = "Error retrieving login details for system "
					+ systemId;
			ASCLogger.error(this.getClass(), methodName, msg, gue);
			throw gue;
		}
	}

	/**
	 * Checks to see if system is entitled or not
	 * @return
	 */
	public boolean isEntitled() {
		if ("Y".equalsIgnoreCase(this.getInsiteEntitlement())
				&& "Y".equalsIgnoreCase(this.getAgreementSigned())
				&& !ASCUtil.isNullOrEmptyObject(insiteCheckedDate)) {
			return true;
		}
		return false;
	}

	private void clear() {
		ipAddress = null;
		modality = null;
		modelType = null;
		location = null;
		usn = null;
		mln = null;
		systemId = null;
		productLine = null;
		loginInfo = null;
		isoCode = null;
		pole = null;
		connectType = null;
		modemAreaCityCode = null;
		modemNum = null;
		systemDetailsExist = false;
		connectivityInfoExist = false;
		loginInfoExist = false;
		allExist = false;
	}

	/**
	 * @return the serviceCenter
	 */
	public String getServiceCenter() {
		return serviceCenter;
	}

	/**
	 * @param serviceCenter
	 *            the serviceCenter to set
	 */
	public void setServiceCenter(String serviceCenter) {
		this.serviceCenter = serviceCenter;
	}

	/**
	 * @return the portMaster
	 */
	public String getPortMaster() {
		return portMaster;
	}

	/**
	 * @param portMaster
	 *            the portMaster to set
	 */
	public void setPortMaster(String portMaster) {
		this.portMaster = portMaster;
	}

	/**
	 * @return the insiteEntitlement
	 */
	public String getInsiteEntitlement() {
		return insiteEntitlement;
	}

	/**
	 * @return the agreementSigned
	 */
	public String getAgreementSigned() {
		return agreementSigned;
	}

	/**
	 * @return the insiteCheckedDate
	 */
	public Date getInsiteCheckedDate() {
		return insiteCheckedDate;
	}

	/**
	 * @return the isIP
	 */
	public boolean isIP() {
		return isIP;
	}

	/**
	 * Get linkSecret (a.k.a ChapSecret)s
	 * @return
	 */
	public String getLinkSecret() {
		return linkSecret;
	}

	/**
	 * Set linkSecret
	 * @param linkSecret
	 */
	public void setLinkSecret(String linkSecret) {
		this.linkSecret = linkSecret;
	}
}
